package net.orange.architecture.system;

/**
 * System sequence number generator.
 */
public class SystemSequence {
	private static final int base = 100000;

	private static long millis = 0, old = 0;

	/**
	 * Get the base Sequence
	 * 
	 * @return The system sequence number. 
	 * @throws SequenceException
	 */
	public static synchronized long getSequence() throws Exception {
		long r = System.currentTimeMillis();

		if (r == millis) {
			old++;

			if (old >= base)
				throw new Exception("Get Sequence error.");

			r = millis * base + old;
		} else {
			millis = r;
			r *= base;
			old = 0;
		}
		return r;
	}
}